package day4.homework;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

//通过一个类自定义的类  属性 任意  方法任意编写
//  通过反射获取类的所有属性和方法  然后 将获取的数据
//  写入到result.txt中  内容如下:
//  方法有:
//  属性有:
//  构造方法有:
public class Food {
    public String name;
    private double price;

    public Food(String name) {
        this.name = name;
    }

    public Food(double price) {
        this.price = price;
    }

    public Food(String name, double price) {
        this.name = name;
        this.price = price;
    }
    public static void showName(String name){
        System.out.println("这份食物的名字是:"+name);
    }
    private static double showPrice(double price){
        System.out.println("食物的价格是:");
        return price;
    }
    public static void eat(){
        System.out.println("吃饱了");
    }

    public static void main(String[] args) throws ClassNotFoundException {
        Class c = Class.forName("day4.homework.Food");
        Method[] ms = c.getDeclaredMethods();
        String MethodStrings = "方法有：\n";
        for (Method m : ms) {
            String name = m.getName();
            Class[] cs = m.getParameterTypes();
            Class result = m.getReturnType();
            MethodStrings +="方法名："+name+" 参数："+ Arrays.toString(cs) +" 返回值："+result+"\n";
        }
        Field[] fs = c.getDeclaredFields();
        String fieldStrings = "属性有：\n";
        for (Field f : fs) {
            fieldStrings +=f.getType() +" "+f.getName()+"\n";
        }
        Constructor[] cons = c.getDeclaredConstructors();
        String constructorStrings = "构造方法有:\n";
        for (Constructor con : cons) {
            constructorStrings += con+"\n";
        }
        String result = MethodStrings+fieldStrings+constructorStrings;
        try (
             FileWriter fw = new FileWriter("d://IO//a//result.txt");
             BufferedWriter bw = new BufferedWriter(fw);
        ){
          bw.write(result);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
